class Solution {
public:
    string replaceSpace(string s) {
        size_t old_len=s.size();
        size_t new_len=0;
        int count=0;//记录空格
        for(size_t i=0;i<old_len;i++)
        {
            if(s[i]==' ')
            {
                count++;;
            }
        }
        new_len=old_len+2*count;//替换一次长度增加2
        s.resize(new_len);
        int old_index=old_len-1;
        int new_index=new_len-1;

        while(old_index>=0&&new_index>=0)
        {
            if(s[old_index]==' ')
            {
                s[new_index--]='0';
                s[new_index--]='2';
                s[new_index--]='%';
                old_index--;
            }
            else
            {
                s[new_index--]=s[old_index--];
            }
        }
        return s;
    }
};